<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
session_start();
if (!isset($_SESSION['Username']) || !isset($_SESSION['Password'])) {
    header('Location: index.php');
}
if ($_GET['gagal'] == 1) {
?>
    <script type="text/javascript">alert("Operation failed");</script>
<?php
}
require_once("database/profile_handler.php");
require_once 'database/photo_handler.php';
require_once 'database/photo_uploader.php';
require_once 'database/follower_handler.php';
$u = $_SESSION['Username'];
$pf = new profile_handler();
$pf->get_nama_lengkap($_SESSION['Username']);
$nama = $pf->result[0];

$pr = new profile_handler();
$pr->retr_details($_SESSION['Username'], 5);
$u = $_SESSION['Username'];
$ph = new photo_handler();

//ambil profpic id
$pf->retr_details($u, 4);
$id = $pf->result[0];


//ambil url
$ph->get_url($id);

if (isset($_GET['edit_follower']) && $_GET['edit_follower'] == 1) {
    $edit_follower = true;
}

if (isset($_GET['edit_following']) && $_GET['edit_following'] == 1) {
    $edit_following = true;
}

if (isset($_GET['del_follower']) && isset($_GET['id_follower']) && $_GET['del_follower'] == 1) {
    $del_followerhandler = new follower_handler();
    $del_followerhandler->del_follower($u, $_GET['id_follower']);
    header("Location: homepage.php?prof=edit_follower");
}

if (isset($_GET['del_following']) && isset($_GET['id_following']) && $_GET['del_following'] == 1) {
    $del_followerhandler = new follower_handler();
    $del_followerhandler->del_follower($_GET['id_following'], $u);
    header("Location: homepage.php?prof=edit_following");
}

if (isset($_GET['addfollowing_from']) && isset($_GET['id_follower'])) {
    $add_following = new follower_handler();
    $add_following->add_follower($_GET['id_follower'], $u);
}

if (isset($_GET['unfollowing_from']) && isset($_GET['id_follower'])) {
    $add_following = new follower_handler();
    $add_following->del_follower($_GET['id_follower'], $u);
}

if (isset($_POST['SubmitEditFollowing'])) {
    if ($_POST['following_people'] != null) {
        // hapus all tagged people
        $del_follower = new follower_handler();
        $del_follower->del_allfollowing($u);
        $array_tag = explode(";", $_POST['following_people']);
        for ($tt = 0; $tt < count($array_tag); $tt++) {
            if ($array_tag[$tt] != $nama) {
                $query = "SELECT usr_email FROM user WHERE nama_lengkap='$array_tag[$tt]'";
                $prof_handler = new profile_handler();
                $prof_handler->exec_q($query, 'usr_email');
                if ($prof_handler->result[0] != null) {
                    $del_follower->add_follower($prof_handler->result[0], $u); // add following, dibalik
                }
            }
        }
    } else {
        // kosongkan semua follower
        $del_follower = new follower_handler();
        $del_follower->del_allfollowing($u);
    }
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>Profile</title>
        <link rel="stylesheet" href="style/style.css" />
        <script type="text/javascript" src="script/validator.js"></script>
        <script type="text/javascript" src="script/calendar.js"></script>
        <script type="text/javascript" src="script/ajax.js"></script>
        <script type="text/javascript">
            document.getElementById("profile_bottom").innerHTML = "lontong";
        </script>
    </head>
    <body onLoad="loadPageHide()" >
        
<?php
if ($edit_follower == null && $edit_following == null) {
?>
    <div class="group_content" id="center_profile">
                <span id="title_group"><?php echo $nama; ?>'s Profile</span>
                <div id="image_profile_center">
                    <img src="<? echo $ph->result[0]; ?>" alt="" width="100"/>
                </div>
                <div id="profile_details">
                    <br />

                    <div id="label_profile"> Birthday</div> <div class="profile_content" id="BirthdaySpan"> : <? echo $pr->result[0][0]; ?></div>
                    <div id="label_profile"> Location</div> <div class="profile_content" id="LocationSpan"> : <? echo $pr->result[1][0]; ?></div>
                    <div id="label_profile"> Hobby</div> <div class="profile_content" id="HobbySpan"> : <? echo $pr->result[2][0]; ?></div>
                    <div id="label_profile"> Wall Privacy</div> <div class="profile_content" id="WallSpan"> : <? echo $pr->result[5][0]; ?></div>
                    <!--a href="homepage.php?viewchangeprof=1" id="headerLink">Change</a-->
                </div>
            </div>
                <div id="follower_profile" class="group_content">
                    <span id="title_group">Follower</span>
                    <div id="display_follower">
<?php
            $follower = new follower_handler();
            $follower->retr_follower($u);
            if ($follower->result[0] != NULL) {
?> <ul class="hoverbox"> <?php
                        for ($l = 0; $l < count($follower->result); $l++) {
                            $photo_handler = new photo_handler();
                            $prof_handler = new profile_handler();
                            $prof_handler->retr_details($follower->result[$l], 4);
                            $photo_handler->get_url($prof_handler->result[0]);
?>
                                <li>
                                    <a href="otherprofile.php?UserTujuan=<?php echo $follower->result[$l] ?>"><img src="<?php echo $photo_handler->result[0]; ?>" alt="description" /><img src="<?php echo $photo_handler->result[0]; ?>" alt="description" class="preview" /></a>
                                </li>
<?php
                        }
?>
                        </ul>
<?php
                    } else {
                        echo "<br />";
                    }
?>
                    </div>
                </div>

                <div id="following_profile" class="group_content">
                    <span id="title_group">Following</span>
                    <div id="display_follower">
<?php
                    $follower = new follower_handler();
                    $follower->retr_following($u);
                    if ($follower->result[0] != NULL) {
?> <ul class="hoverbox"> <?php
                        for ($l = 0; $l < count($follower->result); $l++) {
                            $photo_handler = new photo_handler();
                            $prof_handler = new profile_handler();
                            $prof_handler->retr_details($follower->result[$l], 4);
                            $photo_handler->get_url($prof_handler->result[0]);
?>
                                <li>
                                    <a href="otherprofile.php?UserTujuan=<?php echo $follower->result[$l] ?>"><img src="<?php echo $photo_handler->result[0]; ?>" alt="description" /><img src="<?php echo $photo_handler->result[0]; ?>" alt="description" class="preview" /></a>
                                </li>
<?php
                        }
?>
                        </ul>
<?php
                    } else {
                        echo "<br />";
                    }

?>
                    </div>
                </div>

<?php
                } else if ($edit_follower) {
?>
                    <div class="group_content" id="center_profile">
                        <span id="title_group">Your Followers</span>
                        <span id="_title" style="float:right; margin-top:5px; margin-right:20px;"><a href="profile.php" alt="">Back</a></span>
                        <div id="follower_profile" class="group_content">
                            <div id="display_follower">
<?php
                    $follower = new follower_handler();
                    $follower->retr_follower($u);
                    if ($follower->result[0] != NULL) {
?> <ul class="hoverbox"> <?php
                        for ($l = 0; $l < count($follower->result); $l++) {
                            $photo_handler = new photo_handler();
                            $prof_handler = new profile_handler();
                            $prof_handler->retr_details($follower->result[$l], 4);
                            $photo_handler->get_url($prof_handler->result[0]);
                            //for ($kkk=0;$kkk<10;$kkk++) {
?>
                                <li>
                                    <a href="otherprofile.php?UserTujuan=<?php echo $follower->result[$l]; ?>"><img src="<?php echo $photo_handler->result[0]; ?>" alt="description" /><img src="<?php echo $photo_handler->result[0]; ?>" alt="description" class="preview" /></a><br/><?php
                            $cek_isfollowing = new follower_handler();
                            if (!$cek_isfollowing->is_follow($u, $follower->result[$l])) {
?><span style="position:relative;left:12px;text-align:center;"><a id="link_name" href="profile.php?edit_follower=1&addfollowing_from=1&id_follower=<?php echo $follower->result[$l]; ?>">Follow</a></span>
                                    <?php
                                } else {
                                    ?>
                                <span style="position:relative;left:5px;text-align:center;"><a id="link_name" href="profile.php?edit_follower=1&unfollowing_from=1&id_follower=<?php echo $follower->result[$l]; ?>">Unfollow</a></span>
<?php
                                }
?>
                            </li>
                            <li id="normal_list"><a id="link_name" href="profile.php?edit_follower=1&del_follower=1&id_follower=<?php echo $follower->result[$l]; ?>">x</a></li>
<?php
                                //}
                                $prof_handler->get_nama_lengkap($follower->result[$l]);
                                $allname_follower .= $prof_handler->result[0];
                                $allname_follower .= ';';
                            }
?>
                        </ul>
<?php
                        } else {
                            echo "<br />";
                        }
?>
                    </div>
                </div>
<?php
                    } else if ($edit_following) {
?>
                        <div class="group_content" id="center_profile">
                            <span id="title_group">Your Following</span>
                            <span id="_title" style="float:right; margin-top:5px; margin-right:20px;"><a href="profile.php" alt="">Back</a></span>
                            <div id="follower_profile" class="group_content">
                                <div id="display_follower">
<?php
                        $follower = new follower_handler();
                        $follower->retr_following($u);
                        if ($follower->result[0] != NULL) {
?> <ul class="hoverbox"> <?php
                            for ($l = 0; $l < count($follower->result); $l++) {
                                $photo_handler = new photo_handler();
                                $prof_handler = new profile_handler();
                                $prof_handler->retr_details($follower->result[$l], 4);
                                $photo_handler->get_url($prof_handler->result[0]);
                                //for ($kkk=0;$kkk<10;$kkk++) {
?>
                                    <li>
                                        <a href="otherprofile.php?UserTujuan=<?php echo $follower->result[$l]; ?>"><img src="<?php echo $photo_handler->result[0]; ?>" alt="description" /><img src="<?php echo $photo_handler->result[0]; ?>" alt="description" class="preview" /></a>
                                    </li>
                                    <li id="normal_list"><a id="link_name" href="profile.php?edit_following=1&del_following=1&id_following=<?php echo $follower->result[$l]; ?>">x</a></li>
<?php
                                //}
                                $prof_handler->get_nama_lengkap($follower->result[$l]);
                                $allname_follower .= $prof_handler->result[0];
                                $allname_follower .= ';';
                            }
?>
                            </ul>
<?php
                        } else {
                            echo "<br />";
                        }
?>
                        </div>
                    </div>
                    <div id="add_follow" class="group_content">
                        <span id="title_group">Add Following</span>
                        <form id="edit_addfollowing" method="post" name="AddFollowingForm" action="profile.php?edit_following=1" enctype="multipart/form-data">
                            <div class="uinputarea">
                                <div id="insert_description">
                                    <textarea class="textarea" id="description_text" name="following_people" rows="5"><?php echo $allname_follower; ?></textarea>
                                </div>
                                <span id="Example" style="position:relative; left:45px;">Ex. Danang Tri Massandy;Achmad Giovani;</span>
                            </div>
                            <input type="submit" id="SubmitAddFollow" name="SubmitEditFollowing" value="Submit"/>
                        </form>
                    </div>
<?php

                }
?>

            </div>
        </div>

    </body>
</html>